<!DOCTYPE html>
<html>
<head>
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?d890b1f16fb364253e79c5bb20225c3a";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>


    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?busuanzi_value_site_uv"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    <meta name="baidu-site-verification" content="FYMCShbUK8" />
    <meta name="baidu-site-verification" content="ZYRF7OxQRW" />
    <meta name="baidu-site-verification" content="cHSqtjI0PN" />
    <meta name="baidu-site-verification" content="cHSqtjI0PN" />
    <meta name="baidu-site-verification" content="cHSqtjI0PN" />
    
    
    <link rel="canonical" href="https://hhardyy.com/2019/08/04/JavaScript常见设计模式/">
    
    
    <title>JavaScript常见设计模式 | 小方块 - hhardyy.com | 复杂的坑+归其原理+了解实现规则===解决？解决成功：加油解决成功;</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#958e93">
    
    
    <meta name="keywords" content="javascript,设计模式,深拷贝">
    <meta name="description" content="javascript常见设计模式">
<meta name="keywords" content="javascript,设计模式,深拷贝">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript常见设计模式">
<meta property="og:url" content="http://yoursite.com/2019/08/04/JavaScript常见设计模式/index.html">
<meta property="og:site_name" content="小方块 - hhardyy.com">
<meta property="og:description" content="javascript常见设计模式">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://yoursite.com/images/jsantd/make4.JPG">
<meta property="og:image" content="http://yoursite.com/images/jsantd/iterator10.JPG">
<meta property="og:image" content="http://yoursite.com/images/jsantd/chain_of_resibilities11.jpg">
<meta property="og:image" content="http://yoursite.com/images/jsantd/14-1.png">
<meta property="og:image" content="http://yoursite.com/images/jsantd/14-2.png">
<meta property="og:image" content="http://yoursite.com/images/jsantd/14-3.png">
<meta property="og:updated_time" content="2020-01-13T15:19:02.243Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript常见设计模式">
<meta name="twitter:description" content="javascript常见设计模式">
<meta name="twitter:image" content="http://yoursite.com/images/jsantd/make4.JPG">
    
        <link rel="alternate" type="application/atom+xml" title="小方块 - hhardyy.com" href="/atom.xml">
    
    <link rel="shortcut icon" href="/hardyfavicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/paulGraham.jpg)">
      <div class="brand" style="background-color:#4154b2">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">BingZhenhuang</h5>
          <a href="mailto:huangbingzhen@hhardyy.com" title="huangbingzhen@hhardyy.com" class="mail">huangbingzhen@hhardyy.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                所有文章
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/HHardyy" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://gitee.com/hhardyy" target="_blank" >
                <i class="icon icon-lg icon-cloud"></i>
                Gitee
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://juejin.im/user/59a26f926fb9a02487553b04"  >
                <i class="icon icon-lg icon-pencil"></i>
                掘金-圳
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://segmentfault.com/u/hhardyy"  >
                <i class="icon icon-lg icon-comments"></i>
                Segmentfault
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://codepen.io/HHardyy/" target="_blank" >
                <i class="icon icon-lg icon-codepen"></i>
                Codepen
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.freecodecamp.cn/hhardyy" target="_blank" >
                <i class="icon icon-lg icon-leaf"></i>
                Freecodecamp
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/友情链接"  >
                <i class="icon icon-lg icon-link"></i>
                友链
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">JavaScript常见设计模式</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">JavaScript常见设计模式</h1>
        <h5 class="subtitle">
            
                <time datetime="2019-08-03T19:05:36.000Z" itemprop="datePublished" class="page-time">
  2019-08-04
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#单例模式"><span class="post-toc-number">1.</span> <span class="post-toc-text">单例模式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用："><span class="post-toc-number">1.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项："><span class="post-toc-number">1.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#构造函数模式"><span class="post-toc-number">2.</span> <span class="post-toc-text">构造函数模式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用：-1"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项：-1"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#建造者模式"><span class="post-toc-number">3.</span> <span class="post-toc-text">建造者模式</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#工厂模式"><span class="post-toc-number">4.</span> <span class="post-toc-text">工厂模式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用：-2"><span class="post-toc-number">4.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项：-2"><span class="post-toc-number">4.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#外观模式"><span class="post-toc-number">5.</span> <span class="post-toc-text">外观模式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用：-3"><span class="post-toc-number">5.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项：-3"><span class="post-toc-number">5.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#代理模式"><span class="post-toc-number">6.</span> <span class="post-toc-text">代理模式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用：-4"><span class="post-toc-number">6.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项：-4"><span class="post-toc-number">6.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#观察者模式"><span class="post-toc-number">7.</span> <span class="post-toc-text">观察者模式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用：-5"><span class="post-toc-number">7.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项：-5"><span class="post-toc-number">7.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#策略模式"><span class="post-toc-number">8.</span> <span class="post-toc-text">策略模式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用：-6"><span class="post-toc-number">8.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项：-6"><span class="post-toc-number">8.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#命令模式"><span class="post-toc-number">9.</span> <span class="post-toc-text">命令模式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用：-7"><span class="post-toc-number">9.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项：-7"><span class="post-toc-number">9.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#迭代器模式"><span class="post-toc-number">10.</span> <span class="post-toc-text">迭代器模式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用：-8"><span class="post-toc-number">10.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项：-8"><span class="post-toc-number">10.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#职责链模式"><span class="post-toc-number">11.</span> <span class="post-toc-text">职责链模式</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#适配器模式"><span class="post-toc-number">12.</span> <span class="post-toc-text">适配器模式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用：-9"><span class="post-toc-number">12.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项：-9"><span class="post-toc-number">12.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#模板方法"><span class="post-toc-number">13.</span> <span class="post-toc-text">模板方法</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用：-10"><span class="post-toc-number">13.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项：-10"><span class="post-toc-number">13.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#原型模式"><span class="post-toc-number">14.</span> <span class="post-toc-text">原型模式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#模式作用：-11"><span class="post-toc-number">14.1.</span> <span class="post-toc-text">模式作用：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#注意事项：-11"><span class="post-toc-number">14.2.</span> <span class="post-toc-text">注意事项：</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-JavaScript常见设计模式"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">JavaScript常见设计模式</h1>
        <div class="post-meta">
            <time class="post-time" title="2019-08-04 03:05:36" datetime="2019-08-03T19:05:36.000Z"  itemprop="datePublished">2019-08-04</time>

            


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>javascript常见设计模式</p>
<p><iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="330" height="86" src="//music.163.com/outchain/player?type=2&id=562613275&auto=0&height=66"></iframe><br><a id="more"></a></p>
<h3 id="单例模式"><a href="#单例模式" class="headerlink" title="单例模式"></a>单例模式</h3><p>单例就是保证一个类只有一个实例，实现的方法一般是先判断实例存在与否，如果存在直接返回，如果不存在就创建了再返回，这就确保了一个类只有一个实例对象。在js中，单例作为一个命名<br>空间提供者，从全局命名空间里提供一个唯一的访问点来访问该对象。</p>
<blockquote>
<p>比如你买了个新房子，房子如果没有门，就创建一个门，如果有门就可以直接从门走出去，每个门归属于独立的房号，房间的住户可以通过门进行单例通信</p>
</blockquote>
<h4 id="模式作用："><a href="#模式作用：" class="headerlink" title="模式作用："></a>模式作用：</h4><p>1、模块间通信<br>2、系统中某个类的对象只能存在一个<br>3、保护自己的属性和方法</p>
<h4 id="注意事项："><a href="#注意事项：" class="headerlink" title="注意事项："></a>注意事项：</h4><p>1、注意this的使用<br>2、闭包容易造成内存泄露，不需要的时候干掉<br>3、注意new的成本（继承）<br>举个栗子，页面上6个按钮的分别点击事件，如果用世界上最强的技术$.的话就是这么写<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">$(<span class="string">'#a'</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="comment">//业务逻辑</span></div><div class="line">&#125;)</div><div class="line">$(<span class="string">'#b'</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="comment">//业务逻辑</span></div><div class="line">&#125;)</div><div class="line">$(<span class="string">'#c'</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="comment">//业务逻辑</span></div><div class="line">&#125;)</div><div class="line">$(<span class="string">'#d'</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="comment">//业务逻辑</span></div><div class="line">&#125;)</div><div class="line">$(<span class="string">'#e'</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="comment">//业务逻辑</span></div><div class="line">&#125;)</div><div class="line">$(<span class="string">'#f'</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="comment">//业务逻辑</span></div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>这么写的话，如果每个点击按钮的业务逻辑特别复杂，就乱掉了，而且如果$(‘#a’).click(function(){})里头声明了一个变量a=1,$(‘#b’).click(function(){})里面是不可能接收到的，因为a已经被$(‘#a’)给闭包了，所以如果用单例模式来开发这种代码，应该将它归属于不同的命名空间，比如：<br>a、b、c归属于tag1,d、e、f归属于tag2<br>采用单例的实现方式就是这样<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> tag1=&#123;</div><div class="line">	init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line"></div><div class="line">    &#125;,</div><div class="line">    a:<span class="number">1</span>,</div><div class="line">    render:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        <span class="keyword">let</span> me=<span class="keyword">this</span></div><div class="line">        me.btna=$(<span class="string">'#a'</span>)</div><div class="line">    &#125;,</div><div class="line">    bind:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        <span class="keyword">let</span> me=<span class="keyword">this</span></div><div class="line">        me.bina.click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">            me.test()</div><div class="line">        &#125;)</div><div class="line">    &#125;,</div><div class="line">    test:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        a=<span class="number">5</span></div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">let</span> tag2=&#123;</div><div class="line">	init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line"></div><div class="line">    &#125;,</div><div class="line">    a:<span class="number">1</span>,</div><div class="line">    render:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        <span class="keyword">let</span> me=<span class="keyword">this</span></div><div class="line">        me.btna=$(<span class="string">'#d'</span>)</div><div class="line">    &#125;,</div><div class="line">    bind:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        <span class="keyword">let</span> me=<span class="keyword">this</span></div><div class="line">        me.bina.click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">            me.test()</div><div class="line">        &#125;)</div><div class="line">    &#125;,</div><div class="line">    test:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        a=<span class="number">6</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>单例通讯的时候先绑定dom<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> tag1=&#123;</div><div class="line">	init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	    <span class="keyword">this</span>.render()</div><div class="line">	    <span class="keyword">this</span>.bind()</div><div class="line">    &#125;</div><div class="line">    ...</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">let</span> tag2=&#123;</div><div class="line">	init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	    <span class="keyword">this</span>.render()</div><div class="line">	    <span class="keyword">this</span>.bind()</div><div class="line">    &#125;,</div><div class="line">    test:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        tag1.a=<span class="number">6</span>      <span class="comment">//改变tag1里面的a</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="构造函数模式"><a href="#构造函数模式" class="headerlink" title="构造函数模式"></a>构造函数模式</h3><p>构造函数用于创建特定类型的对象—不仅声明了使用的对象，构造函数还可以接受参数以便第一次创建对象的时候设置对象的成员值。可以自定义自己的构造函数，然后在里面声明自定义类型<br>对象的属性或方法。</p>
<p>在js中，构造函数通常是认为用来实现实例的，js没有类的概念，但是有特殊的构造函数。通过new关键字来调用自定义的构造函数，在构造函数内部，this关键字引用的是新创建的对象</p>
<h4 id="模式作用：-1"><a href="#模式作用：-1" class="headerlink" title="模式作用："></a>模式作用：</h4><p>1、用于创建特定类型的对象<br>2、第一次声明的时候给对象赋值<br>3、自己声明构造函数，赋予属性和方法</p>
<h4 id="注意事项：-1"><a href="#注意事项：-1" class="headerlink" title="注意事项："></a>注意事项：</h4><p>1、声明函数的时候处理业务逻辑<br>2、区分和单例的区别，配合单例实现初始化<br>3、构造函数大写字母开头<br>4、注意new的成本<br>代码实现其实就是构造函数<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">hardy</span>(<span class="params">age</span>)</span>&#123;</div><div class="line">	<span class="keyword">if</span>(!(<span class="keyword">this</span> <span class="keyword">instanceof</span> hardy))&#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">new</span> hardy()</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">this</span>.age=age?age:<span class="string">'90'</span></div><div class="line">    <span class="keyword">this</span>.create=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.age</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">let</span> har=hardy(<span class="number">18</span>)</div><div class="line"><span class="keyword">let</span> har1=hardy1(<span class="number">19</span>)</div><div class="line"><span class="built_in">console</span>.log(har)   <span class="comment">//18</span></div><div class="line"><span class="built_in">console</span>.log(har1)   <span class="comment">//19</span></div></pre></td></tr></table></figure></p>
<p>单例模式与构造函数结合<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> A=&#123;</div><div class="line">	<span class="function"><span class="keyword">function</span>：<span class="title">hardy</span>(<span class="params">age</span>)</span>&#123;</div><div class="line">    	<span class="keyword">if</span>(!(<span class="keyword">this</span> <span class="keyword">instanceof</span> hardy))&#123;</div><div class="line">            <span class="keyword">return</span> <span class="keyword">new</span> hardy()</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">this</span>.age=age?age:<span class="string">'90'</span></div><div class="line">        <span class="keyword">this</span>.create=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.age</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="keyword">let</span> B=&#123;</div><div class="line">	<span class="function"><span class="keyword">function</span>：<span class="title">hardy</span>(<span class="params">age</span>)</span>&#123;</div><div class="line">    	<span class="keyword">if</span>(!(<span class="keyword">this</span> <span class="keyword">instanceof</span> hardy))&#123;</div><div class="line">            <span class="keyword">return</span> <span class="keyword">new</span> hardy()</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">this</span>.age=age?age:<span class="string">'90'</span></div><div class="line">        <span class="keyword">this</span>.create=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">            <span class="keyword">return</span> <span class="keyword">this</span>.age</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">let</span> har=<span class="keyword">new</span> AA.hardy(<span class="number">18</span>)</div><div class="line"><span class="keyword">let</span> har1=<span class="keyword">new</span> BB.hardy1(<span class="number">19</span>)</div><div class="line"><span class="built_in">console</span>.log(har)   <span class="comment">//18</span></div><div class="line"><span class="built_in">console</span>.log(har1)   <span class="comment">//19</span></div></pre></td></tr></table></figure></p>
<h3 id="建造者模式"><a href="#建造者模式" class="headerlink" title="建造者模式"></a>建造者模式</h3><p>建造者模式可以将一个复杂对象的构建与其表示相分离，是的同样的构建过程可以创建不同的表示。也就是说如果我们用了建造者模式，那么用户就需要指定需要建造的类型就可以得到它们，建造者模式实际就是一个指挥者，一个建造者，一个使用指挥者调用具体建造者工作得出结果的客户。</p>
<p>建造者模式主要用于“分步骤构建一个复杂的对象”，在这其中“分步骤”是一个稳定的算法，而复杂对象的各个部分则经常变化</p>
<p>比如大家再熟悉不过的$.ajax，<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$.ajax(&#123;        <span class="comment">//这里的$.ajax就是建造者</span></div><div class="line">    url:<span class="string">'hardy.php'</span>,</div><div class="line">    succ:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;,</div><div class="line">    err:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>假设：<br>1、产出的东西是房子<br>2、baogongtou调用工人进行开工，而且他要很清楚工人们的某一个大项<br>3、工人是盖房子的工人可以建造卧室、客厅、厨房<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">House</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="keyword">this</span>.room=<span class="string">""</span></div><div class="line">	<span class="keyword">this</span>.living_room=<span class="string">""</span></div><div class="line">	<span class="keyword">this</span>.kitchen=<span class="string">""</span></div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Foreman</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="keyword">this</span>.gaifangzi=<span class="function"><span class="keyword">function</span>(<span class="params">worker</span>)</span>&#123;</div><div class="line">    	worker.make_room()</div><div class="line">    	worker.make_livingroom()</div><div class="line">    	worker.make_kitchen()</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Worker</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="keyword">this</span>.make_room=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        <span class="built_in">console</span>.log(<span class="string">'卧室我建好了'</span>)</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">this</span>.make_livingroom=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">         <span class="built_in">console</span>.log(<span class="string">'客厅我建好了'</span>)</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">this</span>.make_kitchen=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        <span class="built_in">console</span>.log(<span class="string">'厨房我建好了'</span>)</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">this</span>.complate=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        <span class="keyword">var</span> _house=<span class="keyword">new</span> House()</div><div class="line">        _house.room=<span class="string">"ok"</span></div><div class="line">        _house.living_room=<span class="string">"ok"</span></div><div class="line">        _house.kitchen=<span class="string">"ok"</span></div><div class="line">        <span class="keyword">return</span> _house</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="keyword">let</span> worker=<span class="keyword">new</span> Worker()</div><div class="line"><span class="keyword">let</span> foreman=<span class="keyword">new</span> Foreman()</div><div class="line">foreman.gaifangzi(worker)</div><div class="line"><span class="keyword">let</span> myhouse=worker.complate()</div><div class="line"><span class="built_in">console</span>.log(<span class="built_in">JSON</span>.stringify(myhouse))</div></pre></td></tr></table></figure></p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/images/jsantd/make4.JPG" alt="" title="">
                </div>
                <div class="image-caption"></div>
            </figure>
<h3 id="工厂模式"><a href="#工厂模式" class="headerlink" title="工厂模式"></a>工厂模式</h3><p>工厂模式定义一个用于创建对象的接口，这个接口由子类决定实例化哪一个类。该模式使一个类的实例化延迟到了子类。而子类可以重写接口方法以便创建的时候指定自己的对象类型（抽象工厂）</p>
<p>这个模式十分有用，尤其是创建对象的流程赋值的时候，比如依赖于很多设置文件等。并且，你会经常在程序里看到工厂方法，用于让子类类定义需要创建的对象类型</p>
<h4 id="模式作用：-2"><a href="#模式作用：-2" class="headerlink" title="模式作用："></a>模式作用：</h4><p>1、对象的构建十分复杂<br>2、需要依赖具体的环境创建不同的实例<br>3、处理大量具有相同属性的小对象</p>
<h4 id="注意事项：-2"><a href="#注意事项：-2" class="headerlink" title="注意事项："></a>注意事项：</h4><p>不能滥用工厂，有时候仅仅是给代码增加复杂度<br>简单工厂<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> XMLHttpFactory=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</div><div class="line">XMLHttpFactory.createXMLHttp=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="keyword">let</span> XMLHttp=<span class="literal">null</span></div><div class="line">	<span class="keyword">if</span> (<span class="built_in">window</span>.XMLHttpRequest) &#123;</div><div class="line">		XMLHttp=<span class="keyword">new</span> XMLHttpRequest()</div><div class="line">	&#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="built_in">window</span>.ActiveXObject)&#123;</div><div class="line">		XMLHttp=<span class="keyword">new</span> ActiveXObject(<span class="string">"Microsoft.XMLHTTP"</span>)</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> XMLHttp</div><div class="line">&#125;</div><div class="line"><span class="keyword">let</span> AjaxHander=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="keyword">let</span> XMLHttp=XMLHttpFactory.createXMLHttp()</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>抽象工厂<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">XMLHttpFactory.prototype=&#123;</div><div class="line">	createFactory:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">    	<span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">'this is an abstract class'</span>)</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="keyword">let</span> XHRHander=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	XMLHttpFactory.call(<span class="keyword">this</span>)</div><div class="line">&#125;</div><div class="line">XHRHander.prototype=<span class="keyword">new</span> XMLHttpFactory()</div><div class="line">XHRHander.prototype.constructor=XHRHander</div><div class="line"></div><div class="line">XHRHander.prototype.createFactory=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="keyword">var</span> XMLHttp=<span class="literal">null</span></div><div class="line">	<span class="keyword">if</span>(<span class="built_in">window</span>.XMLHttpRequest)&#123;</div><div class="line">    	XMLHttp=<span class="keyword">new</span> XMLHttpRequest()</div><div class="line">    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(<span class="built_in">window</span>.ActiveXObject(<span class="string">"Microsoft.XMLHTTP)&#123;</span></div><div class="line"><span class="string">        XMLHttp=new ActiveXObject('Microsoft.XMLHTTP')</span></div><div class="line"><span class="string">    &#125;</span></div><div class="line"><span class="string">    return XMLHttp</span></div><div class="line"><span class="string">&#125;</span></div></pre></td></tr></table></figure></p>
<h3 id="外观模式"><a href="#外观模式" class="headerlink" title="外观模式"></a>外观模式</h3><p>外观模式（Facade）为子系统中的一组接口提供了一个一致的界面，此模块定义了一个高层接口，这个接口使得这一子系统更加容易使用。<br>外观模式不仅简化类中的接口，而且对接口与调用者也进行了解耦。外观模式经常被认为开发者必备，它可以将一些复杂操作封装起来，并创建一个简单的接口用于调用</p>
<h4 id="模式作用：-3"><a href="#模式作用：-3" class="headerlink" title="模式作用："></a>模式作用：</h4><p>1、在设计初期，应该要意识地将不同的两个层分离，比如经典的三层结构。<br>2、在开发阶段，子系统往往因为不断的重构演化而变得越来越复杂，增加外观可以提供一个简单的接口，减少他们之间的依赖。<br>3、在维护一个遗留的大型系统时，为系统开发一个外观Facade类，为设计粗糙和高度复杂的遗留代码提供比较清晰的接口，让新系统和Facade对象交互。</p>
<h4 id="注意事项：-3"><a href="#注意事项：-3" class="headerlink" title="注意事项："></a>注意事项：</h4><p>1、外观模式被开发者连续使用时会产生一定的性能问题，因为在每次调用时都要检测功能的可用性</p>
<blockquote>
<p>代码</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> stopEvent=<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;   <span class="comment">//阻止冒泡和默认事件</span></div><div class="line">	e.stopPropagation()</div><div class="line">	e.preventDefault()</div><div class="line">&#125;</div><div class="line">$(<span class="string">'.btn'</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;</div><div class="line">	stopEvent(e)</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<h3 id="代理模式"><a href="#代理模式" class="headerlink" title="代理模式"></a>代理模式</h3><p>代理，顾名思义就是帮助别人做事，GoF对代理模式的定义如下：<br>代理模式（Proxy），为其他对象提供一种代理以控制对这个对象的访问。<br>代理模式使得代理对象控制具体对象的引用。代理几乎可以是任何对象：文件，资源，内存中的对象，或者是一些难以复制的东西</p>
<h4 id="模式作用：-4"><a href="#模式作用：-4" class="headerlink" title="模式作用："></a>模式作用：</h4><p>1、远程代理（一个对象将不同空间的对象进行局部代理）<br>2、虚拟代理（根据需要创建开销很大的对象如渲染网页暂时用占位符代替真图）<br>3、安全代理（控制真实对象的访问权限）<br>4、智能指引（调用对象代理处理另外一些事情如垃圾回收机制）</p>
<h4 id="注意事项：-4"><a href="#注意事项：-4" class="headerlink" title="注意事项："></a>注意事项：</h4><p>不能滥用代理，有时候仅仅是给代码增加复杂度</p>
<blockquote>
<p>代码</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">maijia</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="keyword">this</span>.name=<span class="string">'小明'</span></div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">zhongjie</span>(<span class="params"></span>)</span>&#123;&#125;</div><div class="line">zhongjie.prototype.maifang=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="keyword">new</span> fangdong(<span class="keyword">new</span> maijia()).maifang(<span class="string">'300'</span>)</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">fangdong</span>(<span class="params">maijia</span>)</span>&#123;</div><div class="line">	<span class="keyword">this</span>.maijia_name=maijia.name</div><div class="line">	<span class="keyword">this</span>.maifang=<span class="function"><span class="keyword">function</span>(<span class="params">money</span>)</span>&#123;</div><div class="line">		<span class="built_in">console</span>.log(<span class="string">`收到<span class="subst">$&#123;<span class="keyword">this</span>.maijia_name&#125;</span>  <span class="subst">$&#123;money&#125;</span>万元`</span>)</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">(<span class="keyword">new</span> zhongjie).maifang()</div></pre></td></tr></table></figure>
<h3 id="观察者模式"><a href="#观察者模式" class="headerlink" title="观察者模式"></a>观察者模式</h3><p>观察者模式又叫发布订阅者模式（Publish/Subscribe），它定义了一种一对多的关系，让多个观察者对象同时监听某一个主题对象，这个主题对象的状态发生变化的时候就会通知所有的观察者对象，使得它们能够自动更新自己</p>
<h4 id="模式作用：-5"><a href="#模式作用：-5" class="headerlink" title="模式作用："></a>模式作用：</h4><p>1、支持简单的广播通信，自动通知所有已经订阅过的对象。<br>2、页面载入后目标对象很容易与观察者存在一种动态关联，增加了灵活性<br>3、目标对象与观察者之间的抽象耦合关系能够扩展以及重用</p>
<h4 id="注意事项：-5"><a href="#注意事项：-5" class="headerlink" title="注意事项："></a>注意事项：</h4><p>监听要在触发之前</p>
<blockquote>
<p>代码</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//在ES5中主要是通过Object.defineProperty方法定义对象属性的设置和获取，并在进行设置时执行相关的处理函数</span></div><div class="line"><span class="comment">// 创建对象</span></div><div class="line"><span class="keyword">var</span> targetObj = &#123;</div><div class="line">    age: <span class="number">1</span></div><div class="line">&#125;</div><div class="line"><span class="comment">// 定义值改变时的处理函数</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">observer</span>(<span class="params">oldVal, newVal</span>) </span>&#123;</div><div class="line">	<span class="comment">// 其他处理逻辑...</span></div><div class="line">    <span class="built_in">console</span>.info(<span class="string">'name属性的值从 '</span>+ oldVal +<span class="string">' 改变为 '</span> + newVal);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 定义name属性及其set和get方法</span></div><div class="line"><span class="built_in">Object</span>.defineProperty(targetObj, <span class="string">'name'</span>, &#123;</div><div class="line">    enumerable: <span class="literal">true</span>,</div><div class="line">    configurable: <span class="literal">true</span>,</div><div class="line">    get: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">        <span class="keyword">return</span> name;</div><div class="line">    &#125;,</div><div class="line">    set: <span class="function"><span class="keyword">function</span>(<span class="params">val</span>) </span>&#123;</div><div class="line">        <span class="comment">//调用处理函数</span></div><div class="line">        observer(name, val)</div><div class="line">        name = val;</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">targetObj.name = <span class="string">'Martin'</span>;</div><div class="line">targetObj.name = <span class="string">'Lucas'</span>;</div><div class="line"><span class="built_in">console</span>.info(<span class="string">'targetObj:'</span>, targetObj)</div><div class="line"></div><div class="line">输出</div><div class="line">name属性的值从 Martin 改变为 Lucas</div><div class="line">targetObj: &#123;<span class="attr">age</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">'Lucas'</span>&#125;</div></pre></td></tr></table></figure>
<p>ES6<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">TargetObj</span> </span>&#123;</div><div class="line">    <span class="keyword">constructor</span>(age, name) &#123;</div><div class="line">        <span class="keyword">this</span>.name = name;</div><div class="line">        <span class="keyword">this</span>.age = age;</div><div class="line">    &#125;</div><div class="line">    set name(val) &#123;</div><div class="line">        observer(name, val);</div><div class="line">        name = val;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">let</span> targetObj = <span class="keyword">new</span> TargetObj(<span class="number">1</span>, <span class="string">'Martin'</span>);</div><div class="line"></div><div class="line"><span class="comment">// 定义值改变时的处理函数</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">observer</span>(<span class="params">oldVal, newVal</span>) </span>&#123;</div><div class="line">	<span class="comment">// 其他处理逻辑...</span></div><div class="line">    <span class="built_in">console</span>.info(<span class="string">'name属性的值从 '</span>+ oldVal +<span class="string">' 改变为 '</span> + newVal);</div><div class="line">&#125;</div><div class="line">targetObj.name = <span class="string">'Lucas'</span>;</div><div class="line"><span class="built_in">console</span>.info(targetObj)</div><div class="line"></div><div class="line">输出</div><div class="line">name属性的值从 Martin 改变为 Lucas</div><div class="line">targetObj: &#123;<span class="attr">age</span>: <span class="number">1</span>, <span class="attr">name</span>: <span class="string">'Lucas'</span>&#125;</div></pre></td></tr></table></figure></p>
<p>proxy Api<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">TargetObj</span> </span>&#123;</div><div class="line">    <span class="keyword">constructor</span>(age, name) &#123;</div><div class="line">        <span class="keyword">this</span>.name = name;</div><div class="line">        <span class="keyword">this</span>.age = age;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">let</span> targetObj = <span class="keyword">new</span> TargetObj(<span class="number">1</span>, <span class="string">'Martin'</span>);</div><div class="line"></div><div class="line"><span class="keyword">let</span> observerProxy = <span class="keyword">new</span> <span class="built_in">Proxy</span>(targetObj, &#123;</div><div class="line">    set(target, property, value, reciever) &#123;</div><div class="line">        <span class="keyword">if</span> (property === <span class="string">'name'</span>) &#123;</div><div class="line">            observer(target[property], value);</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="built_in">Reflect</span>.set(target, property, value, reciever);</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line"><span class="comment">// 定义值改变时的处理函数</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">observer</span>(<span class="params">oldVal, newVal</span>) </span>&#123;</div><div class="line">	<span class="comment">// 其他处理逻辑...</span></div><div class="line">    <span class="built_in">console</span>.info(<span class="string">`name属性的值从 <span class="subst">$&#123;oldVal&#125;</span> 改变为 <span class="subst">$&#123;newVal&#125;</span>`</span>);</div><div class="line">&#125;</div><div class="line"></div><div class="line">observerProxy.name = <span class="string">'Lucas'</span>;</div><div class="line"><span class="built_in">console</span>.info(targetObj);</div></pre></td></tr></table></figure></p>
<h3 id="策略模式"><a href="#策略模式" class="headerlink" title="策略模式"></a>策略模式</h3><p>策略模式定义了算法家族，分别封装起来，让他们之间可以相互替换，此模式让算法的变化不会影响到使用算法的客户</p>
<h4 id="模式作用：-6"><a href="#模式作用：-6" class="headerlink" title="模式作用："></a>模式作用：</h4><p>1、所有的这些算法都是做相同的事情，只是实现不同<br>2、以相同的方式调用所有的方法，减少了各种算法类与使用算法类之间的耦合<br>3、单独定义算法类，也方便了单元测试</p>
<h4 id="注意事项：-6"><a href="#注意事项：-6" class="headerlink" title="注意事项："></a>注意事项：</h4><p>不仅可以封装算法，也可以用来封装几乎任何类型的规则，是要在分析过程中需要在不同时间应用不同的业务规则，就可以考虑是要策略模式来处理各种变化</p>
<blockquote>
<p>代码：</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//比如验证一个电话号码输入</span></div><div class="line"><span class="keyword">let</span> val = <span class="built_in">document</span>.querySelector(<span class="string">'#input'</span>);</div><div class="line"></div><div class="line"><span class="keyword">let</span> valLint=&#123;</div><div class="line">	isEmpty:<span class="function"><span class="keyword">function</span>(<span class="params">num</span>)</span>&#123;</div><div class="line">	    <span class="keyword">return</span> boolean</div><div class="line">    &#125;,</div><div class="line">    isTel:<span class="function"><span class="keyword">function</span>(<span class="params">num</span>)</span>&#123;</div><div class="line">        <span class="keyword">return</span> boolean</div><div class="line">    &#125;</div><div class="line">&#125;<span class="comment">//这是一个策略对象，也可以将所用的所有策略分别写入valLint里面，然后封装成一个.js文件，在需要用到策略的时候进行require然后传值判断，直接得到需要的boolean</span></div><div class="line"></div><div class="line"><span class="comment">//使用策略</span></div><div class="line"><span class="keyword">let</span> isEmpty=valLint.isEmpty(val)</div><div class="line"><span class="keyword">let</span> isTel=valLint.isTel(val)</div><div class="line"></div><div class="line"><span class="keyword">if</span>(!isEmpty&amp;&amp;isTel)&#123;</div><div class="line">	....</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="命令模式"><a href="#命令模式" class="headerlink" title="命令模式"></a>命令模式</h3><p>命令模式(Command)的定义是：用来对方法调用进行参数化处理和传送，经过这样处理过的方法调用可以在任何需要的时候执行。也就是说该模式旨在将函数的调用、请求和操作封装成一个单一的对象，然后对这个对象进行一系列的处理。它也可以用来消除调用操作的对象和实现操作的对象之间的耦合。这为各种具体的类的更换带来了极大的灵活性。</p>
<h4 id="模式作用：-7"><a href="#模式作用：-7" class="headerlink" title="模式作用："></a>模式作用：</h4><p>1、将函数的封装、请求、调用结合为一体<br>2、调用具体的函数解耦命令对象与接收对象<br>3、提高程序模块化的灵活性</p>
<h4 id="注意事项：-7"><a href="#注意事项：-7" class="headerlink" title="注意事项："></a>注意事项：</h4><p>不需要接口一致，直接调用函数即可，以免造成浪费</p>
<blockquote>
<p>代码：</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> lian=&#123;&#125;</div><div class="line"></div><div class="line">lian.bubing=<span class="function"><span class="keyword">function</span>(<span class="params">bu_num</span>)</span>&#123;</div><div class="line">	<span class="built_in">console</span>.log(<span class="string">`步兵<span class="subst">$&#123;bu_num&#125;</span>开始战斗`</span>)</div><div class="line">&#125;</div><div class="line">lian.paobing=fnuction(pao_num)&#123;</div><div class="line">	<span class="built_in">console</span>.log(<span class="string">`炮兵<span class="subst">$&#123;pao_num&#125;</span>开始战斗`</span>)</div><div class="line">&#125;</div><div class="line">lian.lianzhang=<span class="function"><span class="keyword">function</span>(<span class="params">mingling</span>)</span>&#123;</div><div class="line">	lian[mingling.type](mingling.num)</div><div class="line">&#125;</div><div class="line"><span class="comment">//发布命令</span></div><div class="line">lian.lianzhang(&#123;</div><div class="line">	type:<span class="string">'bubing'</span>,</div><div class="line">	num:<span class="number">100</span></div><div class="line">&#125;)</div><div class="line">lian.lianzhang(&#123;</div><div class="line">	type:<span class="string">'paobing'</span>,</div><div class="line">	num:<span class="number">50</span></div><div class="line">&#125;)</div><div class="line"><span class="comment">//步兵100开始战斗，炮兵50开始战斗</span></div></pre></td></tr></table></figure>
<h3 id="迭代器模式"><a href="#迭代器模式" class="headerlink" title="迭代器模式"></a>迭代器模式</h3><p>迭代器模式提供一种方法顺序访问一个聚合对象中各个元素，而又不需要暴露该方法中的内部表示</p>
<h4 id="模式作用：-8"><a href="#模式作用：-8" class="headerlink" title="模式作用："></a>模式作用：</h4><p>1、为遍历不同的集合结构提供一个统一的接口，从而支持同样的算法在不同的集合结构上进行操作（比如jquery中的$.each()）<br>2、集合内部尝尝变化各异，不想暴露其内部结构，但是又想让客户代码透明地访问其中的元素，就可以用迭代器模式。</p>
<h4 id="注意事项：-8"><a href="#注意事项：-8" class="headerlink" title="注意事项："></a>注意事项：</h4><p>1、一般的迭代，至少需要有2个方法，hasNext()和Next()，这样做才做到遍历所有的对象。<br>2、遍历的同时更改迭代器所在的集合结构可能会导致问题（比如c#的foreach里不允许修改item）</p>
<blockquote>
<p>代码：</p>
</blockquote>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/images/jsantd/iterator10.JPG" alt="code" title="">
                </div>
                <div class="image-caption">code</div>
            </figure>
<h3 id="职责链模式"><a href="#职责链模式" class="headerlink" title="职责链模式"></a>职责链模式</h3><p>职责链模式是使多个对象都有机会处理请求，从而避免请求的发送者和接收者之间的耦合关系。将这个对象连城一条链，并沿着这条链传递该请求，直到有一个对象处理它为止。<br>（举个栗子：客户需要开发一个系统，于是联系大boss，大boss肯定不会自己写代码，于是大boss把任务指派给项目经理，项目经理自己有几率不写，于是指派给coder来写代码,在这个过程中，客户没有直接和coder说，而是通过大boss层层指派，形成一条链，并沿着这条链传递该任务，上面说的对象指的就是我们这些coder）</p>
<p>链中收到请求的对象要么亲自处理它，要么转发给下一个候选者。提交方并不明确有多少个对象会处理它，任一候选者都可以响应的请求，可以在运行时刻决定哪些候选者参与到链中。</p>
<blockquote>
<p>代码：</p>
</blockquote>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/images/jsantd/chain_of_resibilities11.jpg" alt="“职责链实现”" title="">
                </div>
                <div class="image-caption">“职责链实现”</div>
            </figure>
<h3 id="适配器模式"><a href="#适配器模式" class="headerlink" title="适配器模式"></a>适配器模式</h3><p>适配器模式（Adapter）是将一个类（对象）的接口（方法或属性）转换成客户希望的另外一个接口（方法或属性），适配器模式使得原本由<br>于接口不兼容而不能一起工作的那些类（对象）可以一起工作。</p>
<h4 id="模式作用：-9"><a href="#模式作用：-9" class="headerlink" title="模式作用："></a>模式作用：</h4><p>1、使用一个已经存在的对象，但其方法或接口不符合你的要求<br>2、创建一个可复用的对象，该对象可以与其他不相关或不可见的对象协同工作<br>3、使用已经存在的一个或多个对象，但是不能继承已匹配它的接口</p>
<h4 id="注意事项：-9"><a href="#注意事项：-9" class="headerlink" title="注意事项："></a>注意事项：</h4><p>与代理模式的区别，代理模式是不改变原接口适配时原接口不符合规范</p>
<blockquote>
<p>举个栗子：假如本来你做的一个项目里头有一个Object</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> a=&#123;</div><div class="line">	test:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;,</div><div class="line">	go:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</div><div class="line">&#125;</div><div class="line">a.test()</div><div class="line"><span class="comment">//这是你原来写的东西，然后假如项目重构，别人嫌弃你的静态方法，于是写成</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">b</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="keyword">this</span>.test=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</div><div class="line">&#125;</div><div class="line">b.prototype.go=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</div><div class="line"><span class="comment">//别人重构之后你所有调a的地方都要写成let a=new b()，然后改着改着自己就乱了</span></div></pre></td></tr></table></figure>
<blockquote>
<p>代码：</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//在别人重构之后，我们可以这样</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Adapter</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="keyword">let</span> h=<span class="keyword">new</span> b()</div><div class="line">	<span class="keyword">let</span> a=&#123;</div><div class="line">    	test:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">        	h.test()</div><div class="line">        &#125;,</div><div class="line">        go:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">            h.go()</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> a</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">let</span> a=Adapter()</div><div class="line">a.test()</div><div class="line">a.go()</div></pre></td></tr></table></figure>
<p>这样就迁移回来了，假如在参加一个比较大的项目的时候有功能一样的a.js和b.js，一般来说要判断<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span>(...)&#123;</div><div class="line">	<span class="built_in">require</span>(<span class="string">'a.js'</span>)</div><div class="line">&#125;<span class="keyword">else</span> <span class="keyword">if</span>(...)&#123;</div><div class="line">	<span class="built_in">require</span>(<span class="string">'b.js'</span>)</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>其实没必要，因为可以封装一个c.js的适配器，然后暴露a,b相应的接口对外，在a,b接口判断相应的逻辑，然后处理a.js和b.js</p>
<h3 id="模板方法"><a href="#模板方法" class="headerlink" title="模板方法"></a>模板方法</h3><p>模板方法(TemplateMethod)定义了一个操作中的算法的骨架，而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。</p>
<p>模板方法是一种代码复用的基本技术，在类库中尤为重要，因为他们提取了类库中的公共行为。模板方法导致一种反向的控制结构，这种结构就是传说中的”好莱坞法则”，即”别找我们，我们找你”，这指的是父类调用一个类的操作，而不是相反。具体体现是面向对象编程语言里面的抽象类（以及其中的抽象方法），以及继承该抽象类（和抽象方法）的子类</p>
<h4 id="模式作用：-10"><a href="#模式作用：-10" class="headerlink" title="模式作用："></a>模式作用：</h4><p>1、一次性实现一个算法的不变的部分，并将可变的行为留给子类来实现<br>2、各子类中公共的行为应该被提取出来并集中到一个公共父类中规避代码重复，不同之处分离为新的操作，最后，用一个钓鱼这些新操作的模板方法来替换这些不同的代码<br>3、控制子类扩展，模板方法只在特定点调用”hook”，这样就允许在这些点进行扩展</p>
<h4 id="注意事项：-10"><a href="#注意事项：-10" class="headerlink" title="注意事项："></a>注意事项：</h4><p>和策略模式不同，模板方法使用继承来改变算法的一部分，而策略模式使用委托来改变整个算法</p>
<blockquote>
<p>实现一个游戏流程：</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Process</span>(<span class="params"></span>)</span>&#123;&#125;</div><div class="line">Process.prototype.start=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	confirm(<span class="string">'开始游戏'</span>)</div><div class="line">&#125;</div><div class="line">Process.prototype.loading=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	<span class="built_in">console</span>.log(<span class="string">'loading...'</span>)</div><div class="line">&#125;</div><div class="line">Process.prototype.out=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">	confirm(<span class="string">'结束游戏'</span>)</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Angry_birds</span>(<span class="params"></span>)</span>&#123;&#125;</div><div class="line"></div><div class="line">Angry_birds.prototype=<span class="keyword">new</span> Process()</div><div class="line">Angry_birds.start()</div></pre></td></tr></table></figure>
<p>上述代码=&gt;建立一个游戏流程的模板，然后有一个愤怒的小鸟的游戏直接继承了该模板，有其他游戏也可以直接继承该游戏模板</p>
<h3 id="原型模式"><a href="#原型模式" class="headerlink" title="原型模式"></a>原型模式</h3><p>原型模式（prototype）是指用原型实例指向创建对象的种类，并且通过拷贝这些原型创建新的对象，这里的prototype不是js里面的prototype，而是一个原型的英文单词，只不过js里面的prototype刚好符合这种原型模式，真正在设计模式里面是有原型模式这样的概念的，所以不能把js里面的prototype和原型模式prototype语法混淆。</p>
<p>对于原型模式，可以利用JavaScript特有的原型继承特性去创建对象的方式，真正的原型继承是作为最新版的ECMAScript5标准提出的，使用Object.create方法来创建这样的对象，比如Object.create(prototype,optionalDescriptorObjects)</p>
<h4 id="模式作用：-11"><a href="#模式作用：-11" class="headerlink" title="模式作用："></a>模式作用：</h4><p>原型对象本身就是有效地利用了每个构造器创建的对象</p>
<h4 id="注意事项：-11"><a href="#注意事项：-11" class="headerlink" title="注意事项："></a>注意事项：</h4><p>1、注意的依然是浅拷贝和深拷贝的问题，免得出现引用问题<br>2、现有的文献里查看原型模式的定义，没有针对JavaScript的，你可能发现很多讲解的都是关于类的，但是现实情况是基于原型继承 的JavaScript完全避免了类（class）的概念</p>
<blockquote>
<p>代码：</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> myobj=&#123;</div><div class="line">        str:<span class="string">'mystring'</span>,</div><div class="line">        num:<span class="number">1</span>,</div><div class="line">        myarr:[</div><div class="line">            <span class="number">30</span>,</div><div class="line">            &#123;<span class="attr">arrgo</span>:<span class="string">'i am arr'</span>&#125;</div><div class="line">        ],</div><div class="line">        myobj1:&#123;</div><div class="line">            innerobj:&#123;</div><div class="line">                test:<span class="number">25</span></div><div class="line">            &#125;,</div><div class="line">            innerstr:<span class="string">'myobjInnerstr'</span></div><div class="line">        &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">let</span> clone=<span class="function"><span class="keyword">function</span>(<span class="params">obj</span>)</span>&#123;</div><div class="line">    <span class="keyword">let</span> obt=&#123;&#125;,k</div><div class="line">    <span class="keyword">for</span>(k <span class="keyword">in</span> obj)&#123;</div><div class="line">        obt[k]=obj[k]</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> obt</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">let</span> result=clone(myobj)</div><div class="line"><span class="built_in">console</span>.log(myobj)</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'----------------------------'</span>)</div><div class="line">result.myobj1.innerstr=<span class="string">""</span></div><div class="line"><span class="built_in">console</span>.log(result)</div><div class="line"><span class="built_in">console</span>.log(myobj)</div></pre></td></tr></table></figure>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/images/jsantd/14-1.png" alt=".log" title="">
                </div>
                <div class="image-caption">.log</div>
            </figure>
<p>克隆之后修改了myobj1里面的innerstr，然而原来的myobj里面的myobj1[innerstr]也改变了，这是因为受到了引用的影响，可以重写一下clone<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> clone=<span class="function"><span class="keyword">function</span>(<span class="params">obj</span>)</span>&#123;</div><div class="line">    <span class="keyword">let</span> obt=&#123;&#125;,k,b</div><div class="line">    <span class="keyword">if</span>((b=(obj <span class="keyword">instanceof</span> <span class="built_in">Array</span>))||obj <span class="keyword">instanceof</span> <span class="built_in">Object</span>)&#123;</div><div class="line">        obt=b?[]:&#123;&#125;</div><div class="line">        <span class="keyword">for</span>(k <span class="keyword">in</span> obj)&#123;</div><div class="line">            <span class="keyword">if</span>((obj[k] <span class="keyword">instanceof</span> <span class="built_in">Array</span>)||(obj[k] <span class="keyword">instanceof</span> <span class="built_in">Object</span>))&#123;</div><div class="line">                obt[k]=clone(obj[k])</div><div class="line">            &#125;<span class="keyword">else</span>&#123;</div><div class="line">                obt[k]=obj[k]</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> obt</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/images/jsantd/14-2.png" alt=".log" title="">
                </div>
                <div class="image-caption">.log</div>
            </figure>
<p>也可以用Object.create来实现，但是结构打印不出来,值可以拿到，这个是为什么我后续再研究一下，毕竟凌晨4点了。。。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> myobj=&#123;</div><div class="line">        str:<span class="string">'mystring'</span>,</div><div class="line">        num:<span class="number">1</span>,</div><div class="line">        myarr:[</div><div class="line">            <span class="number">30</span>,</div><div class="line">            &#123;<span class="attr">arrgo</span>:<span class="string">'i am arr'</span>&#125;</div><div class="line">        ],</div><div class="line">        obj:&#123;</div><div class="line">            innerobj:&#123;</div><div class="line">                test:<span class="number">25</span></div><div class="line">            &#125;,</div><div class="line">            innerstr:<span class="string">'myobjInnerstr'</span></div><div class="line">        &#125;</div><div class="line">&#125;</div><div class="line"><span class="keyword">let</span> m=<span class="built_in">Object</span>.create(myobj)</div><div class="line"><span class="built_in">console</span>.log(myobj)</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'---------------'</span>)</div><div class="line">m.num=<span class="number">666</span></div><div class="line"><span class="built_in">console</span>.log(m.num)</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'---------------'</span>)</div><div class="line"><span class="built_in">console</span>.log(myobj)</div></pre></td></tr></table></figure></p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/images/jsantd/14-3.png" alt=".log" title="">
                </div>
                <div class="image-caption">.log</div>
            </figure>
<p>！！！总之！！！！！！不要为了设计模式而设计模式！！！</p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-01-13T15:19:02.243Z" itemprop="dateUpdated">2020-01-13 23:19:02</time>
</span><br>


        
        谢谢浏览，我会继续努力的，示例：<a href="/2019/08/04/JavaScript常见设计模式/" target="_blank" rel="external">http://yoursite.com/2019/08/04/JavaScript常见设计模式/</a>
        
    </div>
    <footer>
        <a href="http://yoursite.com">
            <img src="/img/avatar.jpg" alt="BingZhenhuang">
            BingZhenhuang
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/javascript/">javascript</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/深拷贝/">深拷贝</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/设计模式/">设计模式</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2019/08/04/JavaScript常见设计模式/&title=《JavaScript常见设计模式》 — 小方块 - hhardyy.com&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2019/08/04/JavaScript常见设计模式/&title=《JavaScript常见设计模式》 — 小方块 - hhardyy.com&source=javascript常见设计模式
" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2019/08/04/JavaScript常见设计模式/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《JavaScript常见设计模式》 — 小方块 - hhardyy.com&url=http://yoursite.com/2019/08/04/JavaScript常见设计模式/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2019/08/04/JavaScript常见设计模式/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2019/08/22/从无到有跑一个react-native/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">react-native</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2019/07/24/浅谈前端架构/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">前端架构</h4>
      </a>
    </div>
  
</nav>



    














</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        🤠 请我喝可乐！
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content" style="width:50%">
        
        <div class="reward-code" style="text-align:center">
            <div style="width:300px;margin:0px auto;">
               <img id="rewardCode" style="width:50%;height:60%;display:block; margin:0px auto;" src="/img/alipay.jpg" alt="支付宝打赏二维码">
               <span style="display:inline-block; margin-bottom:20px;">0.88(支付宝 aliPay)</span>
               <img id="rewardCode" style="width:50%;height:60%;display:block; margin:0px auto;" src="/img/wechat.jpg" alt="微信打赏二维码">
               <span style="display:inline-block;">0.88(微信 weChat)</span>
            </div>
        </div>
    </div>
</div>



</div>

        <script>
!function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
</script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<script>
    function secondToDate(second) {
        if (!second) {
            return 0;
        }
        var time = new Array(0, 0, 0, 0, 0);
        if (second >= 365 * 24 * 3600) {
            time[0] = parseInt(second / (365 * 24 * 3600));
            second %= 365 * 24 * 3600;
        }
        if (second >= 24 * 3600) {
            time[1] = parseInt(second / (24 * 3600));
            second %= 24 * 3600;
        }
        if (second >= 3600) {
            time[2] = parseInt(second / 3600);
            second %= 3600;
        }
        if (second >= 60) {
            time[3] = parseInt(second / 60);
            second %= 60;
        }
        if (second > 0) {
            time[4] = second;
        }
        return time;
    }</script>
<script type="text/javascript" language="javascript">
    function setTime() {
        var create_time = Math.round(new Date(Date.UTC(2017, 08, 18, 11, 42, 23)).getTime() / 1000);
        var timestamp = Math.round((new Date().getTime() + 8 * 60 * 60 * 1000) / 1000);
        currentTime = secondToDate((timestamp - create_time));
        currentTimeHtml = 'Running：' + currentTime[0] + '年 ' + currentTime[1] + '天 '
                + currentTime[2] + '时 ' + currentTime[3] + '分 ' + currentTime[4]
                + '秒';
        document.getElementById("htmer_time").innerHTML = currentTimeHtml;
    }    setInterval(setTime, 1000);
</script>
<footer class="footer">
    <div class="top">
        

        <p>
          <span id="busuanzi_container_page_pv">
             [&nbsp;浏览量：&nbsp;<span id="busuanzi_value_page_pv"></span>&nbsp;]
          </span>
        </p>
    </div>
    <div class="bottom">
        <p>
        <span>BingZhenhuang &copy; 2017 - 2020</span>
            <span>
                
                Power by <a href="https://hhardyy.github.io/" target="_blank">zhen On August 8</a> 
            </span>
            <span id="htmer_time" "></span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2019/08/04/JavaScript常见设计模式/&title=《JavaScript常见设计模式》 — 小方块 - hhardyy.com&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2019/08/04/JavaScript常见设计模式/&title=《JavaScript常见设计模式》 — 小方块 - hhardyy.com&source=javascript常见设计模式
" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2019/08/04/JavaScript常见设计模式/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《JavaScript常见设计模式》 — 小方块 - hhardyy.com&url=http://yoursite.com/2019/08/04/JavaScript常见设计模式/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2019/08/04/JavaScript常见设计模式/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>








<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '(•‾̑⌣‾̑•)✧˖°回来看我';
            clearTimeout(titleTime);
        } else {
            document.title = '(゜-゜)つロ欢迎回来';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
